<?php
/**
 * Created by PhpStorm.
 * User: stupboy
 * Date: 2018/6/1
 * Time: 13:55
 * Mail:87658989@qq.com
 */

namespace app\api\model;

use think\Db;

class Company extends Common
{
    /**
     * 获取用车单位 单位额度/财政额度
     * @param $pid
     * @param $status
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUseMoneyByPassenger($pid,$status=0){
        $statusArr = [
            '1,2,4,5,6,7,8,9,10,11',// 全部占用额度
            '2,4,5,6,7,8,9,10,11',  // 审批完成占用额度
            '10,11'                 // 支付确认占用额度
        ];
        // 公务有时间范围
        $field     = 'sum(case when a.order_type = 1 and a.pay_type = 3 then passenger_money else 0 end) office_money,';
        $field    .= "sum(case when a.order_type = 1 and a.pay_type = 3 and a.order_state in ('9,10,11') then passenger_money else 0 end) office_money_paid,";
        $field    .= 'sum(case when a.order_type = 1 and a.pay_type = 3 and a.order_state = 9 then passenger_money else 0 end) office_wait_pay,';
        $field    .= "sum(case when a.order_type = 1 and a.pay_type = 3 and a.order_state in ('2,4,5,6,7,8') then pre_fee else 0 end) office_lock_money";
        $passenger = Db::name('passenger')->where('passenger_id',$pid)->find($pid);
        $carUse    = $this->find($passenger['company_id']);
        $list1      = Db::name('order')->alias('a')
            ->join('order_fee d','a.order_id = d.order_id')
            ->where('a.company_id',$carUse['company_id'])
            ->where('order_time','between time',['2017-12-16 00:00:00','2018-12-31'])
            ->where('a.order_state','in',$statusArr[$status])
            ->field($field)
            ->find();
        // 单位无时间范围
        $field     = 'sum(case when a.order_type between 1 and 2 and a.pay_type = 2 then passenger_money else 0 end) company_money,';
        $field    .= "sum(case when a.order_type between 1 and 2 and a.pay_type = 2 and a.order_state in ('9,10,11') then passenger_money else 0 end) company_money_paid,";
        $field    .= 'sum(case when a.order_type between 1 and 2 and a.pay_type = 2 and a.order_state = 9 then passenger_money else 0 end) company_wait_pay,';
        $field    .= "sum(case when a.order_type between 1 and 2 and a.pay_type = 2 and a.order_state in ('2,4,5,6,7,8') then pre_fee else 0 end) company_lock_money";
        $list2      = Db::name('order')->alias('a')
            ->join('order_fee d','a.order_id = d.order_id')
            ->where('a.company_id',$carUse['company_id'])
            ->where('a.order_state','in',$statusArr[$status])
            ->field($field)
            ->find();
        $list = array_merge($list2,$list1);
        $list['quota']      = $carUse['quota'];
        $list['unit_quota'] = $carUse['unit_quota'];
        !isset($list['office_money']) && $list['office_money'] = 0;
        !isset($list['company_money']) && $list['company_money'] = 0;
        // 更新用车单位使用额度
        $data['used_quota']      = $list['office_money_paid'];
        $data['used_unit_quota'] = $list['company_money_paid'];
        $this->save($data,['company_id'=>$passenger['company_id']]);
        $list['company_id']      = $passenger['company_id'];
        return $list;
    }

    /**
     * 获取公司已用额度等
     * @param $company_id
     * @param int $status
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUseMoneyByCompany($company_id,$status=0){
        $statusArr = [
            '1,2,4,5,6,7,8,9,10,11',// 全部占用额度
            '2,4,5,6,7,8,9,10,11',  // 审批完成占用额度
            '10,11'                 // 支付确认占用额度
        ];
        $field     = 'sum(case when a.order_type = 1 and a.pay_type = 3 then passenger_money else 0 end) office_money,';
        $field    .= "sum(case when a.order_type = 1 and a.pay_type = 3 and a.order_state between 9 and 11 then passenger_money else 0 end) office_money_paid,";
        $field    .= 'sum(case when a.order_type = 1 and a.pay_type = 3 and a.order_state = 9 then passenger_money else 0 end) office_wait_pay,';
        $field    .= "sum(case when a.order_type = 1 and a.pay_type = 3 and a.order_state in ('2,4,5,6,7,8') then pre_fee else 0 end) office_lock_money";
        $carUse    = $this->find($company_id);
        $list1      = Db::name('order')->alias('a')
            ->join('order_fee d','a.order_id = d.order_id')
            ->where('a.company_id',$carUse['company_id'])
            ->where('order_time','between time',['2017-12-16 00:00:00','2018-12-31'])
            ->where('a.order_state','in',$statusArr[$status])
            ->field($field)
            ->find();

        $field     = 'sum(case when a.order_type between 1 and 2 and a.pay_type = 2 then passenger_money else 0 end) company_money,';
        $field    .= "sum(case when a.order_type between 1 and 2 and a.pay_type = 2 and a.order_state between 9 and 11 then passenger_money else 0 end) company_money_paid,";
        $field    .= 'sum(case when a.order_type between 1 and 2 and a.pay_type = 2 and a.order_state = 9 then passenger_money else 0 end) company_wait_pay,';
        $field    .= "sum(case when a.order_type between 1 and 2 and a.pay_type = 2 and a.order_state in ('2,4,5,6,7,8') then pre_fee else 0 end) company_lock_money";
        $list2      = Db::name('order')->alias('a')
            ->join('order_fee d','a.order_id = d.order_id')
            ->where('a.company_id',$carUse['company_id'])
            ->where('a.order_state','in',$statusArr[$status])
            ->field($field)
            ->find();
        $list = array_merge($list2,$list1);
        $list['quota']      = $carUse['quota'];
        $list['unit_quota'] = $carUse['unit_quota'];
        !isset($list['office_money']) && $list['office_money'] = 0;
        !isset($list['company_money']) && $list['company_money'] = 0;
        $data['used_quota']      = $list['office_money_paid'];
        $data['used_unit_quota'] = $list['company_money_paid'];
        $this->save($data,['company_id'=>$company_id]);
        $list['company_id']      = $company_id;
        return $list;
    }
}